const Order = require('@models/order');
const OrderItem = require('@models/order-item');
const User = require('@models/user');
const { Op } = require('sequelize');

// 获取订单列表
exports.list = async (req, res, next) => {
  try {
    const {
      page = 1,
      pageSize = 10,
      order_no,
      status,
      start_time,
      end_time
    } = req.query;

    const where = {};

    if (order_no) {
      where.order_no = {
        [Op.like]: `%${order_no}%`
      };
    }

    if (status !== undefined) {
      where.status = status;
    }

    if (start_time && end_time) {
      where.create_time = {
        [Op.between]: [start_time, end_time]
      };
    }

    const { count, rows } = await Order.findAndCountAll({
      where,
      include: [{
        model: User,
        as: 'user',
        attributes: ['id', 'name', 'phone']
      }],
      order: [['id', 'DESC']],
      offset: (page - 1) * pageSize,
      limit: parseInt(pageSize)
    });

    res.json({
      code: 200,
      message: '获取成功',
      data: {
        total: count,
        items: rows,
        page: parseInt(page),
        pageSize: parseInt(pageSize)
      }
    });
  } catch (err) {
    next(err);
  }
};

// 获取订单详情
exports.detail = async (req, res, next) => {
  try {
    const { id } = req.params;

    const order = await Order.findByPk(id, {
      include: [
        {
          model: User,
          as: 'user',
          attributes: ['id', 'name', 'phone']
        },
        {
          model: OrderItem,
          as: 'items'
        }
      ]
    });

    if (!order) {
      return res.status(404).json({
        code: 404,
        message: '订单不存在'
      });
    }

    res.json({
      code: 200,
      message: '获取成功',
      data: order
    });
  } catch (err) {
    next(err);
  }
};

// 发货
exports.deliver = async (req, res, next) => {
  try {
    const { id } = req.params;
    const { express_company, express_no } = req.body;

    const order = await Order.findByPk(id);
    if (!order) {
      return res.status(404).json({
        code: 404,
        message: '订单不存在'
      });
    }

    if (order.status !== 1) {
      return res.status(400).json({
        code: 400,
        message: '订单状态不正确'
      });
    }

    await order.update({
      status: 2,
      delivery_time: new Date(),
      express_company,
      express_no
    });

    res.json({
      code: 200,
      message: '发货成功'
    });
  } catch (err) {
    next(err);
  }
};

// 取消订单
exports.cancel = async (req, res, next) => {
  try {
    const { id } = req.params;
    const { cancel_reason } = req.body;

    const order = await Order.findByPk(id);
    if (!order) {
      return res.status(404).json({
        code: 404,
        message: '订单不存在'
      });
    }

    if (order.status !== 0) {
      return res.status(400).json({
        code: 400,
        message: '订单状态不正确'
      });
    }

    await order.update({
      status: 4,
      cancel_reason
    });

    res.json({
      code: 200,
      message: '取消成功'
    });
  } catch (err) {
    next(err);
  }
}; 